//
//  GWNavigaitionView.m
//  shandsHotel
//
//  Created by zh_s on 2018/9/25.
//  Copyright © 2018年 zh_s. All rights reserved.
//

#import "GWNavigaitionView.h"


static NSInteger leftViewTag = 33;
@implementation GWNavigaitionView

- (instancetype)initWithTitle:(NSString *)title withLeftTitle:(NSString *)leftTitle {
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, STATUS_BAR_HEIGHT+44);
        
        UIImageView *backImageView = [[UIImageView alloc]init];
        backImageView.frame = CGRectMake(0, 0, SCREEN_WIDTH, STATUS_BAR_HEIGHT+44);
        backImageView.image = IMAGE_NAME(@"nav_back");
        [self addSubview:backImageView];
        _backImageView = backImageView;
        
        UILabel *titleLabel = [[UILabel alloc]init];
        titleLabel.frame = CGRectMake(60, STATUS_BAR_HEIGHT, SCREEN_WIDTH -120 ,44);
//        titleLabel.backgroundColor = RandomColor;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.text = title;
        titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:18];
        titleLabel.textColor = MAIN_BACKCOLOR;
        [self addSubview:titleLabel];
        _titleLabel = titleLabel;
        
        [self createLeftViewWith:leftTitle];
    }
    return self;
}
- (void)createLeftViewWith:(NSString *)leftTitle {
    if (!IsStrEmpty(leftTitle)) {
        UIView *leftView;
        CGFloat width;
        CGFloat height;
        if ([leftTitle hasSuffix:@"png"]) {
            NSArray *strArray = [leftTitle componentsSeparatedByString:@"."];
            UIImageView *imageView = [[UIImageView alloc]init];
            [imageView setImage:IMAGE_NAME(strArray[0])];
            leftView = imageView;
            width = 10;
            height = 18;
            
        }else {
            UILabel *label = [[UILabel alloc]init];
            label.font = [UIFont systemFontOfSize:16];
            label.textColor = YELLOWER_COLOR;
            label.text = leftTitle;
//            label.textAlignment = NSTextAlignmentRight;
            leftView = label;
            width = SCREEN_WIDTH/4-10;
            height = 44;
//            UILabel *label = [[UILabel alloc]init];
//            label.font = [UIFont systemFontOfSize:16];
//            label.textColor = [UIColor blackColor];
//            label.text = leftTitle;
//            leftView = label;
        }
        [self addSubview:leftView];
        leftView.tag = leftViewTag;
        leftView.userInteractionEnabled = YES;
        UIView *bigLeftV = [[UIView alloc] init];
        [self addSubview:bigLeftV];
        [bigLeftV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(WIDTH_CONSTARIN(10));
            //            make.top.mas_equalTo(STATUS_BAR_HEIGHT +10);
            make.centerY.mas_equalTo(_titleLabel.mas_centerY);
            //            make.size.mas_equalTo(CGSizeMake(44, 44));
            make.size.mas_equalTo(CGSizeMake(80, NAV_HEIGHT));
        }];
        
        UITapGestureRecognizer *ges = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(leftViewGes)];
        [bigLeftV addGestureRecognizer:ges];
        [leftView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(WIDTH_CONSTARIN(20));
//            make.top.mas_equalTo(STATUS_BAR_HEIGHT +10);
            make.centerY.mas_equalTo(_titleLabel.mas_centerY);
//            make.size.mas_equalTo(CGSizeMake(44, 44));
            make.size.mas_equalTo(CGSizeMake(width, height));
        }];
        _leftView = leftView;
    }
}
- (void)setLeftTitle:(NSString *)leftTitle {
    if (_leftTitle != leftTitle) {
        _leftTitle = leftTitle;
    }
    UIView *oldLeftView = [self viewWithTag:leftViewTag];
    [oldLeftView removeFromSuperview];
    [self createLeftViewWith:leftTitle];
}
- (void)setRightTitle:(NSString *)rightTitle {
    if (_rightTitle != rightTitle) {
        _rightTitle = rightTitle;
    }
    
    CGFloat width;
    CGFloat height;
    if (!IsStrEmpty(rightTitle)) {
        UIView *rightView;
        if ([rightTitle hasSuffix:@".png"] || [rightTitle hasSuffix:@".jpg"]) {
            UIImageView *imageView = [[UIImageView alloc]init];
            NSArray *array = [rightTitle componentsSeparatedByString:@"."];
            imageView.image = IMAGE_NAME(array[0]);
            rightView = imageView;
            width = 44;
            height = 44;
//            width = 10;
//            height = 18;
        }else {
            UILabel *label = [[UILabel alloc]init];
            label.font = [UIFont systemFontOfSize:16];
            label.textColor = MAIN_BACKCOLOR;
            label.text = rightTitle;
            label.textAlignment = NSTextAlignmentRight;
            rightView = label;
            width = SCREEN_WIDTH/4-10;
            height = 44;
        }
        [self addSubview:rightView];
        rightView.tag = 997788;
        rightView.userInteractionEnabled = YES;
        UITapGestureRecognizer *ges = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(rightViewGes)];
        [rightView addGestureRecognizer:ges];
        
        [rightView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.offset(-WIDTH_CONSTARIN(20));
            make.top.offset(STATUS_BAR_HEIGHT);
            make.size.mas_equalTo(CGSizeMake(width,height));
        }];
        _rightView = rightView;
    }else {
        UIView *rightView = [self viewWithTag:997788];
        [rightView removeFromSuperview];
        _rightView = rightView;
    }
    
}
- (void)setBackgroundColor:(UIColor *)backgroundColor {
    _backImageView.image = IMAGE_NAME(@"");
    _backImageView.backgroundColor = backgroundColor;
    _backgroundColor = backgroundColor;
}
- (void)setTitleView:(UIView *)titleView {
    _titleView = titleView;
    [self addSubview:titleView];
}
- (void)rightViewGes {
    if (self.delegate && [self.delegate respondsToSelector:@selector(rightViewClick)]) {
        [self.delegate rightViewClick];
    }
}

- (void)leftViewGes {
    if (self.delegate && [self.delegate respondsToSelector:@selector(leftViewClick)]) {
        [self.delegate leftViewClick];
    }
}


@end
